<?php

class Admin_Controllers_Product extends Libs_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $listPro = new Admin_Models_Product();
        if (isset($_GET['search'])) {
            $this->view->listPro = $listPro->getSearch($_GET['search']);
            $this->view->render("product/index");
        } else {
            $this->view->listPro = $listPro->getAllProduct();
            $this->view->render("product/index");
        }
    }

    public function proSales($pro_id) {
        $sale = new Admin_Models_Sale();
        $this->view->proId = $pro_id;
        $this->view->proSale = $sale->getSaleByProId($pro_id);
        $this->view->render('sale/index');
    }

    public function addSale($pro_id) {
        $sale = new Admin_Models_Sale();

        $sale->setProId($pro_id);
        $sale->setDateStart(date('Y-m-d H:i:s', strtotime(str_replace('/', '-', $_POST['txtDateStart']))));
        $sale->setDateEnd(date('Y-m-d H:i:s', strtotime(str_replace('/', '-', $_POST['txtDateEnd']))));
        $sale->setPercentDecrease($_POST['txtPD']);
        $sale->setContent($_POST['txtContent']);

        $sale->insertSale($sale);

        header("Location: " . URL_BASE . "/admin/product/proSales/" . $pro_id);
    }

    public function editSale($sale_id) {
        $pro_id = $_POST['pro_id'];
        $sale = new Admin_Models_Sale();

        $sale->setProId($pro_id);
        $sale->setDateStart(date('Y-m-d H:i:s', strtotime(str_replace('/', '-', $_POST['txtDateStart']))));
        $sale->setDateEnd(date('Y-m-d H:i:s', strtotime(str_replace('/', '-', $_POST['txtDateEnd']))));
        $sale->setPercentDecrease($_POST['txtPD']);
        $sale->setContent($_POST['txtContent']);

        $sale->editSale($sale, $sale_id);

        header("Location: " . URL_BASE . "/admin/product/proSales/" . $pro_id);
    }

    public function delSale($sale_id) {
        $pro_id = $_GET['pro_id'];
        $sale = new Admin_Models_Sale();
        $sale->delSale($sale_id);

        header("Location: " . URL_BASE . "/admin/product/proSales/" . $pro_id);
    }

    public function proImages($pro_id) {
        $image = new Admin_Models_Image();
        $this->view->proId = $pro_id;
        $this->view->listImages = $image->getImages($pro_id);
        $this->view->render("image/index");
    }

    public function addImages($pro_id) {
        if (isset($_FILES['fImages'])) {
            $images_name = $_FILES['fImages']['name'];
            $images_type = $_FILES['fImages']['type'];
            $images_tmp_name = $_FILES['fImages']['tmp_name'];
            $images_size = $_FILES['fImages']['size'];
            $images_error = $_FILES['fImages']['error'];

            foreach ($images_name as $names) {
                $n[] = $names;
            }
            foreach ($images_tmp_name as $tmp_names) {
                $tmp_name[] = $tmp_names;
            }
            foreach ($images_error as $errors) {
                $error[] = $errors;
            }

            for ($i = 0; $i < count($n); $i++) {
                if ($error[$i] == 0) {
                    if (!file_exists($n[$i])) {
                        move_uploaded_file($tmp_name[$i], "templates/default/products/images/$n[$i]");
                        $iurl = "templates/default/products/images/$n[$i]";

                        $data = array(
                            'pro_id' => $pro_id,
                            'url' => "$iurl"
                        );
                        $img = new Admin_Models_Image();
                        $img->insertImages($data);
                    }
                }
            }
        }
        header("location: " . URL_BASE . "/admin/product/proImages/" . $pro_id);
    }

    public function delImage($img_id) {
        $pro_id = $_GET['pro_id'];
        $img = new Admin_Models_Image();
        $img->deleteImage($img_id);

        header("location: " . URL_BASE . "/admin/product/proImages/" . $pro_id);
    }

    public function delPro($pro_id) {
        $pro = new Admin_Models_Product();
        while ($img = $pro->delete($pro_id)) {
            if (!empty($img)) {
                unlink(URL_BASE . DS . $img['url']);
            }
        }

        header("Location: " . URL_BASE . "/admin/product");
    }

    public function delProducts() {
        $listPro = array();
        $listPro = $_POST['checkOne'];
        $pro = new Admin_Models_Product();
        foreach ($listPro as $proId) {
            while ($img = $pro->delete($proId)) {
                if (!empty($img)) {
                    unlink(URL_BASE . DS . $img['url']);
                }
            }
        }

        header("Location: " . URL_BASE . "/admin/product");
    }

    public function editForm($pro_id) {
        $pro = new Admin_Models_Product();
        $this->view->proData = $pro->getProById($pro_id);
        $this->view->render('product/edit-form');
    }

    public function editPro($pro_id) {
        $e = new Admin_Models_Product();

        $thumb_name = $_FILES['fThumb']['name'];
        $thumb_type = $_FILES['fThumb']['type'];
        $thumb_tmp_name = $_FILES['fThumb']['tmp_name'];
        $thumb_error = $_FILES['fThumb']['error'];
        $thumb_size = $_FILES['fThumb']['size'];

        $thumb = $_POST['txtThumb'];
        $id = $_POST['txtProId'];

        if ($thumb_error == 0) {
            if (!file_exists($thumb_name)) {
                if (!empty($_POST['txtThumb'])) {
                    unlink(URL_BASE . DS . $_POST['txtThumb']);
                }
                $target = "templates/default/products/thumbs/$thumb_name";
                move_uploaded_file($thumb_tmp_name, $target);
                $thumb = $target;
            }
        }

        $e->setName($_POST['txtName']);
        $e->setThumb($thumb);
        $e->setCatId($_POST['sltCatId']);
        $e->setDescription($_POST['txtDescription']);
        $e->setPrice($_POST['txtPrice']);
        $e->setStatus($_POST['sltStatus']);
        $e->setPostDate($_POST['txtPostDate']);

        $e->edit($e, $id);

        header("location: " . URL_BASE . '/admin/product/editForm/' . $id);
    }

    public function insertForm() {
        $this->view->render('product/insert-form');
    }

    public function insertPro() {
        $i = new Admin_Models_Product();

        $thumb_name = $_FILES['fThumb']['name'];
        $thumb_type = $_FILES['fThumb']['type'];
        $thumb_tmp_name = $_FILES['fThumb']['tmp_name'];
        $thumb_size = $_FILES['fThumb']['size'];
        $thumb_error = $_FILES['fThumb']['error'];

        if ($thumb_error == 0) {
            if (!file_exists($thumb_name)) {
                $target = "templates/default/products/thumbs/$thumb_name";
                move_uploaded_file($thumb_tmp_name, $target);
                $thumb = $target;
            }
        }

        $i->setName($_POST['txtName']);
        $i->setThumb($thumb);
        $i->setCatId($_POST['sltCatId']);
        $i->setDescription($_POST['txtDescription']);
        $i->setPrice($_POST['txtPrice']);
        $i->setStatus($_POST['sltStatus']);
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $i->setPostDate(date("Y-m-d h:i:s", time()));

        $lastId = $i->insert($i);

        if (isset($_FILES['fImages'])) {
            $images_name = $_FILES['fImages']['name'];
            $images_type = $_FILES['fImages']['type'];
            $images_tmp_name = $_FILES['fImages']['tmp_name'];
            $images_size = $_FILES['fImages']['size'];
            $images_error = $_FILES['fImages']['error'];

            foreach ($images_name as $names) {
                $n[] = $names;
            }
            foreach ($images_tmp_name as $tmp_names) {
                $tmp_name[] = $tmp_names;
            }
            foreach ($images_error as $errors) {
                $error[] = $errors;
            }

            for ($i = 0; $i < count($n); $i++) {
                if ($error[$i] == 0) {
                    if (!file_exists($n[$i])) {
                        move_uploaded_file($tmp_name[$i], "templates/default/products/images/$n[$i]");
                        $iurl = "templates/default/products/images/$n[$i]";

                        $data = array(
                            'pro_id' => $lastId,
                            'url' => "$iurl"
                        );
                        $img = new Admin_Models_Image();
                        $img->insertImages($data);
                    }
                }
            }
        }
        header("location: " . URL_BASE . "/admin/product");
    }

}
